package com.tanhua.server.controller;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Classname LoginController
 * @Description TODO 用户控制层
 * @Date 2021/3/6 18:04
 * @Created by gan
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserService userService;
   /* //新增用户
    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    public ResponseEntity saveUser(@RequestBody Map<String,Object> param){
        String mobile = (String) param.get("mobile");
        String password = (String) param.get("password");
         userService.saveUser(mobile,password);
        return ResponseEntity.ok("保存成功");
    }
    //根据手机号码查询
    @RequestMapping(value = "/findUser",method = RequestMethod.GET)
    public ResponseEntity findUser(String mobile){
        return userService.findByMobile(mobile);
    }*/

    //用户登录发送验证码
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> param) {
        String phone = param.get("phone");
        userService.sendValidateCode(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 登陆验证码校验
     * @param param
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> param){
        String phone = param.get("phone");
        String verificationCode = param.get("verificationCode");
        Map<String, Object> map = userService.loginVerification(phone, verificationCode);
        return ResponseEntity.ok(map);
    }

    /**
     * 个人信息
     * @param userInfoVo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfoVo userInfoVo, @RequestHeader("Authorization") String token){
        UserInfo userInfo = new UserInfo();
        // 复制属性
        BeanUtils.copyProperties(userInfoVo,userInfo);
        // 保存用户信息
        userService.saveUserInfo(userInfo,token);
        return ResponseEntity.ok(null);
    }

    /**
     * 上传用户头像
     * @param headPhoto
     * @param token
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity uploadAvatar(MultipartFile headPhoto, @RequestHeader("Authorization") String token){
        userService.updateUserAvatar(headPhoto,token);
        return ResponseEntity.ok(null);
    }
}
